import type { Unit, Action } from "@/seed-application/types";
import { open } from '@/components/Popup'
import feedback from '@/utils/feedback'
import { setApi, removeApi, resetApi } from "@/seed-application/api";
import Container from "./Container";
import { getComponent } from ".";

export function doAction(action: Action, unit: Unit, row?: Record<string, any>) {
  if(action.onClick) {
    action.onClick(action, unit, row)
    return
  }

  switch(action.type) {
    case 'save':
      save(action, unit)
      break;
    case 'update':
      update(action, unit, row)
      break;
    case 'detail':
      detail(action, unit, row)
      break;
    case 'remove':
      remove(action, unit, row)
      break;
    case 'get':
      get(action, unit, row)
      break;
    case 'set':
      set(action, unit, row)
      break;
    case 'reset':
      reset(action, unit, row);
      break;
    case 'stat':
      stat(action, unit);
      break
    default:
      feedback.alert('不支持的行为，请确认是否缺少类型')
  }
}

export function save(action: Action, unit: Unit) {
  const props: Record<string, any> = {
    ...(action.componentProps || {}),
    actionName: action.name,
    seedName: action.seed.name,
    microName: action.micro?.name
  }
  console.log('-----------', action)
  const component = action.layout ? Container : getComponent(action.type, action.component)
  const componentProps = action.layout ? { layout: action.layout, props } : props
  
  const title = action.popupProps?.title || action.label || `新增${action.seed.label}`
  const popupProps = { ...(action.popupProps || {}), title, unit }
  open(popupProps, component, componentProps)
}

export async function update(action: Action, unit: Unit, row?: Record<string, any>) {
  const currentRow = row || unit.currentRow
  if(!currentRow) {
    feedback.alertSelect()
    return
  }

  const props: Record<string, any> = {
    ...(action.componentProps || {}),
    actionName: action.name,
    seedName: action.seed.name,
    microName: action.micro?.name,
    id: currentRow.id
  }

  const component = action.layout ? Container : getComponent(action.type, action.component)
  const componentProps = action.layout ? { layout: action.layout, props } : props

  const title = action.popupProps?.title || action.label || `编辑${action.seed.label}`
  const popupProps = { ...(action.popupProps || {}), title, unit }
  open(popupProps, component, componentProps)
}

export async function detail(action: Action, unit: Unit, row?: Record<string, any>) {
  const currentRow = row || unit.currentRow
  if(!currentRow) {
    feedback.alertSelect()
    return
  }
  
  const props: Record<string, any> = {
    ...(action.componentProps || {}),
    actionName: action.name,
    seedName: action.seed.name,
    microName: action.micro?.name,
    id: currentRow.id
  }
  
  const component = action.layout ? Container : getComponent(action.type, action.component)
  const componentProps = action.layout ? { layout: action.layout, props } : props

  const title = action.componentProps?.title || action.label || `${action.seed.label}详情`
  const popupProps = { ...(action.popupProps || {}), title, unit }
  open(popupProps, component, componentProps)
}

export async function remove(action: Action, unit: Unit, row?: Record<string, any>) {
  const currentRow = row || unit.currentRow
  if(!currentRow) {
    feedback.alertSelect()
    return
  }

  try {
    await feedback.confirmRemove()
    await removeApi(action, currentRow.id)
    unit.refresh?.()
  } catch (error) {console.log('error==', error)
    //
  }
}

export async function get(action: Action, unit: Unit, row?: Record<string, any>) {
  const currentRow = row || unit.currentRow
  if(!currentRow) {
    feedback.alertSelect()
    return
  }

  const props: Record<string, any> = {
    ...(action.componentProps || {}),
    actionName: action.name,
    seedName: action.seed.name,
    microName: action.micro?.name,
    id: currentRow.id
  }

  const component = action.layout ? Container : getComponent(action.type, action.component)
  const componentProps = action.layout ? { layout: action.layout, props } : props

  const title = action.popupProps?.title || action.label || `设置${action.seed.label}`
  const popupProps = { ...(action.popupProps || {}), title, unit }
  open(popupProps, component, componentProps)
}

export async function set(action: Action, unit: Unit, row?: Record<string, any>) {
  const currentRow = row || unit.currentRow
  if(!currentRow) {
    feedback.alertSelect()
    return
  }

  if(action.declareValue) {
    try {
      await feedback.confirm(`确定要${action.label}吗?`)
      await setApi(action, currentRow.id)
      unit.refresh?.()
      feedback.msgSuccess(`${action.label}成功`);
    } catch (error) {
      //
    }
    return
  }

  const props: Record<string, any> = {
    ...(action.componentProps || {}),
    actionName: action.name,
    seedName: action.seed.name,
    microName: action.micro?.name,
    id: currentRow.id
  }

  const component = action.layout ? Container : getComponent(action.type, action.component)
  const componentProps = action.layout ? { layout: action.layout, props } : props

  const title = action.popupProps?.title || action.label || `设置${action.seed.label}`
  const popupProps = { ...(action.popupProps || {}), title, unit }
  open(popupProps, component, componentProps)
}

export async function reset(action: Action, unit: Unit, row?: Record<string, any>) {
  const currentRow = row || unit.currentRow
  if(!currentRow) {
    feedback.alertSelect()
    return
  }

  try {
    await feedback.confirm(`确定要${action.label}吗?`)
    await resetApi(action, currentRow.id)
    unit.refresh?.()
    feedback.msgSuccess(`${action.label}成功`);
  } catch (error) {
    //
  }
}

export async function stat(action: Action, unit: Unit) {
  const props: Record<string, any> = {
    ...(action.componentProps || {}),
    actionName: action.name,
    seedName: action.seed.name,
    microName: action.micro?.name
  }
  
  const component = action.layout ? Container : getComponent(action.type, action.component)
  const componentProps = action.layout ? { layout: action.layout, props } : props
  
  const title = action.popupProps?.title || action.label || `统计${action.seed.label}`
  const popupProps = { ...(action.popupProps || {}), title, unit }
  open(popupProps, component, componentProps)
}
